package com.hub.controller.asset;


import com.hub.controller.BaseController;
import com.hub.dto.asset.PartsInfoDto;
import com.hub.dto.asset.SearchPartsInfoDto;
import com.hub.utils.ApiResultObject;
import com.hub.utils.PagingResultObject;
import com.hub.service.asset.IPartsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  配件基础信息前端控制器
 * </p>
 *
 * @author hub
 * @since 2020-05-18
 */
@Slf4j
@Api(value="配件基础信息相关API",tags={"配件基础信息" })
@RestController
@RequestMapping("/asset/parts/info")
public class PartsInfoController extends BaseController {

    @Autowired
    private IPartsInfoService partsInfoService;

    @ApiOperation("查询配件基础信息")
    @PostMapping(value = "/list")
    public PagingResultObject searchPartsInfoList(@RequestBody SearchPartsInfoDto request){

        return partsInfoService.searchPartsInfoList(request);
    }

    @ApiOperation("配件基础信息详情")
    @GetMapping(value = "/{id}")
    public ApiResultObject getPartsInfo(@PathVariable long id){
        return this.partsInfoService.getPartsInfoDetail(id);
    }

    @ApiOperation("新增配件基础信息")
    @PostMapping(value = "")
    public ApiResultObject addPartsInfo(@RequestBody @Valid PartsInfoDto dto, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }

        return partsInfoService.addPartsInfo(dto);
    }

    @ApiOperation("修改配件基础信息")
    @PutMapping(value = "")
    public ApiResultObject modifyPartsInfo(@RequestBody @Valid PartsInfoDto dto, BindingResult bindingResult){
        //数据验证
        ApiResultObject validResult = this.checkValidResult(bindingResult);
        if(!validResult.isSuccess()){
            return validResult;
        }

        if(dto.getId()< 1)
            return ApiResultObject.failed("参数异常: 配件信息ID异常!");

        return partsInfoService.modifyPartsInfo(dto);
    }

    @ApiOperation("删除配件基础信息")
    @DeleteMapping(value = "/{id}")
    public ApiResultObject removePartsInfo(@PathVariable long id){

        return partsInfoService.removeAssetInfo(id);
    }

}

